import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)

import processRouter from './modules/ProcessApplication'

//内容页面注册
import Login from '@/pages/Login/Login'
import ActivationLogin from '@/pages/Login/ActivationLogin'
import PageNotFound from '@/pages/errorPage/404' //404页面
import NoAuthority from '@/pages/errorPage/401'  //401页面

import Layout from '@/pages/layout/index'
import AdminHome from '@/pages/DashBoard/DashBoard'
import ChangePassword from '@/pages/DashBoard/ChangePassword'
import UserManagement from '@/pages/UserManagement/UserManagement'
import UserDetail from '@/pages/UserManagement/UserDetail'
import UserCreate from '@/pages/UserManagement/UserCreate'

import ProcessList from '@/pages/process/ProcessList'
import ProcessDetail from '@/pages/process/ProcessDetail'
import ProcessCreate from '@/pages/process/create'

import AssetsManagement from '@/pages/AssetsManagement/AssetsManagement'
import AssetsLog from '@/pages/AssetsManagement/AssetsLog'
import AssetsUpdate from '@/pages/AssetsManagement/AssetsUpdate'
import AssetsDetail from '@/pages/AssetsManagement/AssetsDetail'
import AssetsCreate from '@/pages/AssetsManagement/AssetsCreate'
import LifeCycle from '@/pages/AssetsManagement/LifeCycle'
import MyAssets from '@/pages/AssetsManagement/MyAssets'
import MyAssetsDetail from '@/pages/AssetsManagement/MyAssetsDetail'

import HardwareManagement from '@/pages/HardwareManagement/HardwareManagement'
import HardwareLog from '@/pages/HardwareManagement/HardwareLog'
import HardwareUpdate from '@/pages/HardwareManagement/Update'
import HardwareCreat from '@/pages/HardwareManagement/Create'

import Classification from '@/pages/SystemSetting/Classification/Classification'
import ClassificationCreate from '@/pages/SystemSetting/Classification/Create.vue'
import ClassificationUpdate from '@/pages/SystemSetting/Classification/Update.vue'
import AttriBute from '@/pages/SystemSetting/Classification/AttriBute/AttriBute'
import AttriButeUpdate from '@/pages/SystemSetting/Classification/AttriBute/Update'
import AttriButeCreate from '@/pages/SystemSetting/Classification/AttriBute/Create'

import FormSetting from '@/pages/SystemSetting/FormSetting/FormSetting'
import FormSettingCreate from '@/pages/SystemSetting/FormSetting/Create.vue'
import FormSettingDetail from '@/pages/SystemSetting/FormSetting/detail.vue'
import FormSettingUpdate from '@/pages/SystemSetting/FormSetting/update.vue'
import FormSettingAttriBute from '@/pages/SystemSetting/FormSetting/FormSettingAttriBute/AttriBute'
import FormSettingAttriButeUpdate from '@/pages/SystemSetting/FormSetting/FormSettingAttriBute/Update'
import FormSettingAttriButeCreate from '@/pages/SystemSetting/FormSetting/FormSettingAttriBute/Create'


import DataStatistics from '@/pages/SystemSetting/DataStatistics/DataStatistics'
import Groups from '@/pages/SystemSetting/Groups/Groups'
import GroupsCreate from '@/pages/SystemSetting/Groups/add'

import SystemLog from '@/pages/SystemSetting/SystemLogs/SystemLog'

import Supplier from '@/pages/SystemSetting/Supplier/Supplier'
import SupplierUpdate from '@/pages/SystemSetting/Supplier/Update'
import SupplierCreate from '@/pages/SystemSetting/Supplier/Create'
import SupplierDetail from '@/pages/SystemSetting/Supplier/Detail'

import StorageLocation from '@/pages/SystemSetting/StorageLocation/StorageLocation'
import StorageLocationCreate from '@/pages/SystemSetting/StorageLocation/Create'
import StorageLocationUpdate from '@/pages/SystemSetting/StorageLocation/Update'

import SystemSetting from '@/pages/SystemSetting/SystemSetting'

import DesktopIcon from '@/pages/ProcessConsole/DesktopIcon'
import history from '@/pages/ProcessConsole/history'
import NewProcessDetail from '@/pages/ProcessConsole/detail'
import form01 from '@/pages/ProcessConsole/ProcessForms/form01'



import FlowConfig from '@/pages/FlowConfig/index'
import FlowDetail from '@/pages/FlowConfig/detail'

import { getToken } from '../utils/auth'
import store from '../store'

const router = new Router({
  mode: 'hash',
  routes: [
    //页面注册
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'Login',
      component: Login,
      meta: {
        title: '管理员登录'
      }
    },
    {
      path: '/ActivationLogin',
      name: 'ActivationLogin',
      component: ActivationLogin,
      meta: {
        title: '激活'
      }
    },
    {
      path: '/AdminHome',
      component: Layout,
      children: [
        {
          path: '/AdminHome',
          name: 'AdminHome',
          meta: {
            title: '管理首页'
          },
          component: AdminHome
        }
      ]
    },
    {
      path: '/AdminHome',
      component: Layout,
      children: [
        {
          path: '/AdminHome/ChangePassword',
          name: 'ChangePassword',
          meta: {
            title: '修改密码'
          },
          component: ChangePassword
        }
      ]
    },
    {
      path: '/ProcessList',
      component: Layout,
      children: [
        {
          path: '/ProcessList',
          name: 'ProcessList',
          meta: {
            title: '查看全部流程'
          },
          component: ProcessList
        },
        {
          path: '/ProcessList/ProcessDetail',
          name: 'ProcessDetail',
          meta: {
            title: '查看全部流程'
          },
          component: ProcessDetail
        },
        {
          path: '/ProcessList/create',
          name: 'create',
          meta: {
            title: '查看新建申请'
          },
          component: ProcessCreate
        }
      ]
    },
    {
      path: '/UserManagement',
      component: Layout,
      children: [
        {
          path: '/UserManagement',
          name: 'UserManagement',
          meta: {
            title: '系统用户管理'
          },
          component: UserManagement
        },
        {
          path: '/UserManagement/UserCreate',
          meta: {
            title: '添加用户'
          },
          component: UserCreate
        },
        {
          path: '/UserManagement/UserDetail',
          meta: {
            title: '用户信息查看'
          },
          component: UserDetail
        }
      ]
    },
    {
      path: '/AssetsManagement',
      component: Layout,
      children: [
        {
          path: '/AssetsManagement',
          name: 'AssetsManagement',
          meta: {
            title: '资产管理'
          },
          component: AssetsManagement
        },
        {
          path: '/AssetsManagement/MyAssets/MyAssetsDetail',
          name: 'MyAssetsDetail',
          meta: {
            title: '资产详情'
          },
          component: MyAssetsDetail
        },
        {
          path: '/AssetsManagement/MyAssets',
          name: 'MyAssets',
          meta: {
            title: '我的资产'
          },
          component: MyAssets
        },
        {
          path: '/AssetsManagement/LifeCycle',
          name: 'LifeCycle',
          meta: {
            title: '查看资产生命周期档案'
          },
          component: LifeCycle
        },
        {
          path: '/AssetsManagement/AssetsUpdate',
          name: 'AssetsUpdate',
          meta: {
            title: '查看资产信息'
          },
          component: AssetsUpdate
        },
        {
          path: '/AssetsManagement/AssetsDetail',
          name: 'AssetsDetail',
          meta: {
            title: '修改资产信息'
          },
          component: AssetsDetail
        },
        {
          path: '/AssetsManagement/AssetsCreate',
          name: 'AssetsCreate',
          meta: {
            title: '添加资产信息'
          },
          component: AssetsCreate
        },
        {
          path: '/AssetsManagement/log',
          name: 'ConsoleLog',
          meta: {
            title: '操作日志'
          },
          component: AssetsLog
        }
      ]
    },
    {
      path: '/HardwareManagement',
      component: Layout,
      children: [
        {
          path: '/HardwareManagement',
          name: 'HardwareManagement',
          meta: {
            title: '设备管理'
          },
          component: HardwareManagement
        },
        {
          path: '/HardwareManagement/Update',
          name: 'HardwareUpdate',
          meta: {
            title: '修改设备信息'
          },
          component: HardwareUpdate
        },
        {
          path: '/HardwareManagement/Create',
          name: 'HardwareCreate',
          meta: {
            title: '新增设备'
          },
          component: HardwareCreat
        },
        {
          path: '/HardwareManagement/log',
          name: 'HardwareLog',
          meta: {
            title: '设备日志'
          },
          component: HardwareLog
        }
      ]
    },
    {
      path: '/SystemSetting',
      redirect: '/SystemSetting/Classification'
    },
    {
      path: '/SystemSetting',
      component: Layout,
      children: [
        {
          path: '/SystemSetting/Classification',
          meta: {
            title: '分类管理'
          },
          component: Classification
        },
        {
          name: 'AttriButeUpdate',
          path: '/SystemSetting/Classification/AttriBute/update',
          meta: {
            title: '修改属性信息'
          },
          component: AttriButeUpdate
        },
        {
          path: '/SystemSetting/Classification/AttriBute/Create',
          meta: {
            title: '添加属性信息'
          },
          component: AttriButeCreate
        },
        {
          path: '/SystemSetting/Classification/AttriBute',
          meta: {
            title: '属性列表'
          },
          component: AttriBute
        },
        {
          path: '/SystemSetting/Classification/update',
          meta: {
            title: '修改分类信息'
          },
          component: ClassificationUpdate
        },
        {
          path: '/SystemSetting/Classification/create',
          meta: {
            title: '分类管理-新建'
          },
          component: ClassificationCreate
        }
      ]
    },


    {
      path: '/SystemSetting',
      component: Layout,
      children: [
        {
          path: '/SystemSetting/FormSetting',
          meta: {
            title: '表单管理'
          },
          component: FormSetting
        },
        {
          name: 'AttriButeUpdate',
          path: '/SystemSetting/FormSetting/FormSettingAttriBute/update',
          meta: {
            title: '修改属性信息'
          },
          component: FormSettingAttriButeUpdate
        },
        {
          path: '/SystemSetting/FormSetting/FormSettingAttriBute/Create',
          meta: {
            title: '添加属性信息'
          },
          component: FormSettingAttriButeCreate
        },
        {
          path: '/SystemSetting/FormSettingAttriBute/AttriBute',
          meta: {
            title: '属性列表'
          },
          component: FormSettingAttriBute
        },
        {
          path: '/SystemSetting/FormSetting/update',
          meta: {
            title: '修改分类信息'
          },
          component: FormSettingUpdate
        },
        {
          path: '/SystemSetting/FormSetting/create',
          meta: {
            title: '分类管理-新建'
          },
          component: FormSettingCreate
        },
        {
          path: '/SystemSetting/FormSetting/detail',
          meta: {
            title: '分类管理-详情'
          },
          component: FormSettingDetail
        }
      ]
    },

    {
      path: '/SystemSetting',
      component: Layout,
      children: [
        {
          path: '/SystemSetting/DataStatistics',
          meta: {
            title: '数据统计'
          },
          component: DataStatistics
        }
      ]
    },
    {
      path: '/SystemSetting',
      component: Layout,
      children: [
        {
          path: '/SystemSetting/Groups',
          meta: {
            title: '组织结构管理'
          },
          component: Groups
        },
        {
          path: '/SystemSetting/Groups/add',
          meta: {
            title: '添加组织'
          },
          component: GroupsCreate
        }
      ]
    },
    {
      path: '/SystemSetting',
      component: Layout,
      children: [
        {
          path: '/SystemSetting/StorageLocation',
          meta: {
            title: '储存位置管理'
          },
          component: StorageLocation
        },
        {
          path: '/SystemSetting/StorageLocation/Create',
          meta: {
            title: '添加储存位置'
          },
          component: StorageLocationCreate
        },
        {
          path: '/SystemSetting/StorageLocation/update',
          meta: {
            title: '修改储存位置信息'
          },
          component: StorageLocationUpdate
        }
      ]
    },
    {
      path: '/SystemSetting',
      component: Layout,
      children: [
        {
          path: '/SystemSetting/SystemLog',
          meta: {
            title: '操作日志'
          },
          component: SystemLog
        }
      ]
    },


    {
      path: '/SystemSetting',
      component: Layout,
      children: [
        {
          path: '/SystemSetting/Supplier',
          meta: {
            title: '供应商管理'
          },
          component: Supplier
        },
        {
          path: '/SystemSetting/Supplier/Update',
          meta: {
            title: '修改供应商信息'
          },
          component: SupplierUpdate
        },
        {
          path: '/SystemSetting/Supplier/Create',
          meta: {
            title: '添加供应商信息'
          },
          component: SupplierCreate
        },
        {
          path: '/SystemSetting/Supplier/Detail',
          meta: {
            title: '查看供应商信息'
          },
          component: SupplierDetail
        }
      ]
    },


    {
      path: '/SystemSetting',
      component: Layout,
      children: [
        {
          path: '/SystemSetting/SystemSetting',
          meta: {
            title: '系统设置'
          },
          component: SystemSetting
        }
      ]
    },
    {
      path: '/ProcessConsole',
      component: Layout,
      children: [
        {
          path: '/ProcessConsole',
          meta: {
            title: '流程管理控制台'
          },
          component: DesktopIcon
        },
        {
          path: '/ProcessConsole/detail',
          meta: {
            title: '流程详情'
          },
          component: NewProcessDetail
        },
        {
          path: '/ProcessConsole/history',
          meta: {
            title: '历史流程'
          },
          component: history
        }
      ]
    },
    {
      path: '/Flow',
      component: Layout,
      children:[
        {
          path: '/Flow',
          meta: {
            title: '流程配置管理'
          },
          component: FlowConfig
        },
        {
          path: '/Flow/detail',
          meta: {
            title: '流程配置查看'
          },
          component: FlowDetail
        }
      ]
    },
    //============测试流程表单==========
    processRouter,

    //配置401页面
    {
      path: '/401',
      name: 'NoAuthority',
      component: NoAuthority,
      meta: {
        title: '没有权限 No Authority'
      }
    },
    //配置404页面
    {
      path: '/404',
      name: '404 not found',
      component: PageNotFound,
      meta: {
        title: '未找到页面'
      }
    },
    {
      path: "*",
      name: '404 not fuound',
      redirect: "/404"
    },
  ],
  //响应路由变化
  watch: {
    '$route'(to, from) {
      // 对路由变化作出响应...
    }
  },
});

//全局路由前置守卫
router.beforeEach((to, from, next) => {
  let isLogin = getToken() !== undefined;
  if (isLogin) {
    if (to.name === 'Login') {
      console.warn("======== 管理员已登录 ========");
      next({ path: '/ProcessConsole' });
    } else {
      next();
    }
  } else {
    if (to.name !== 'Login') {
      next({ path: '/login' });
    } else {
      next();
    }
  }
  //储存当前路径到vuex
  store.dispatch('setActivePath', to.path);
});
//全局路由后置钩子
router.afterEach((to, from) => {
  //路由结束
  window.document.title = to.meta.title;
});

export default router;